package main

import "fmt"

func RomanToInt(s string) int {
	//当组合边数字时,即前面罗马字符对应的数值小于后面罗马数值对应的数值，进行减法
	//IV = 4 = V-I = 0-I+V
	//IX = 9 = X-I = 0-I+X
	//XL = 40 = L-X = 0-X+L
	//不变数字时，即前面罗马字符对应的数值大于后面罗马数值对应的数值，进行加法
	//LV = 55 = 50 +5 = V+L
	roman2Int:=map[string]int{
		"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000,
	}
	var n = 0
	for i := 0; i < len(s)-1; i++ {
		if roman2Int[string(s[i])] < roman2Int[string(s[i+1])]{
			n -= roman2Int[string(s[i])]
		}else {
			n += roman2Int[string(s[i])]
		}
	}
	return n + roman2Int[string(s[len(s)-1])]
}

func main() {
	fmt.Println(RomanToInt("LVIII"))
}